package com.casic.modules.activiti.service;

import com.casic.modules.sys.entity.SysDeptEntity;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysDeptService;
import com.casic.modules.sys.service.SysUserService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Activiti Group Entity Service
 * @author ThinkGem
 * @version 2013-12-05
 */
@Service("actGroupEntityService")
public class ActGroupEntityService extends GroupEntityManager {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDeptService sysDeptService;
	public GroupEntity findGroupById(final String groupCode) {
	        if (groupCode == null)
	            return null;
	            try {
					SysDeptEntity sysDept=sysDeptService.queryObject(groupCode);
	                GroupEntity e = new GroupEntity();  
	                e.setRevision(1);  
	                // activiti有3种预定义的组类型：security-role、assignment、user  
	                // 如果使用Activiti  
	                // Explorer，需要security-role才能看到manage页签，需要assignment才能claim任务  
	                e.setType("assignment");  
	                e.setId(sysDept.getDeptId()+"");
	                e.setName(sysDept.getName());
	                return e;  
	            } catch (EmptyResultDataAccessException e) {
	                return null;  
	            }     
	    }  
	  
	    @Override  
	    public List<Group> findGroupsByUser(final String userCode) {  
	    	if (userCode == null){
	        	 return null;
	        }
			SysUserEntity sysUser=sysUserService.queryObject(userCode);
			SysDeptEntity sysDept=sysDeptService.queryObject(sysUser.getDeptId()+"");
	        List<Group> listGroup=new ArrayList<Group>();
	        GroupEntity groupEntity = new GroupEntity();
	        if(sysDept!=null){
				groupEntity.setId(sysDept.getDeptId()+"");
				groupEntity.setName(sysDept.getName());
				listGroup.add(groupEntity);
			}
	        return listGroup;
	    }  
	  
	    @Override  
	    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {  
	        throw new RuntimeException("not implement method.");  
	    }  
	  
	    @Override  
	    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {  
	        throw new RuntimeException("not implement method.");  
	    }  
}